package com.wd.gui;

import com.wd.dominio.CentroDistribucion;
import com.wd.dominio.Lugar;
import com.wd.gui.controlparticular.ControlGuiCentroDistribucion;
import com.wd.gui.controlparticular.ControlGuiLugar;
import java.util.Vector;

/**
 *
 * @author Gabylis
 */
public class VentanaModificarCentro extends javax.swing.JFrame {

    private Vector<Lugar> estados;

    private Vector<Lugar> ciudades;

    private Vector<Lugar> ciudadesAux;

    private Vector<CentroDistribucion> vecCentros;

    private Vector<CentroDistribucion> vecCentrosAux;

    private ControlGui controlGeneral;

    private ControlGuiLugar controlLugar;

    private ControlGuiCentroDistribucion controlCentro;

    /** Creates new form VentanaModificarCentro */
    public VentanaModificarCentro(Vector<Lugar> result) {
        initComponents();
        java.net.URL url = getClass().getResource("Iconos/icon_016.png");
        java.awt.Image imagen = getToolkit().getImage(url);
        setIconImage (imagen);

        vecCentros = new Vector();
        vecCentrosAux = new Vector();
        ciudadesAux = new Vector();
        ciudades = new Vector();
        estados = new Vector();

        controlCentro = new ControlGuiCentroDistribucion();
        vecCentros = controlCentro.traerTodosLosCentros();

        estados = result;

        for (Lugar lugar : result){
            this.comboEstado.addItem(lugar.getNombrePropio());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        comboEstado = new javax.swing.JComboBox();
        comboCiudad = new javax.swing.JComboBox();
        comboNombre = new javax.swing.JComboBox();
        campoNombre = new javax.swing.JTextField();
        campoTelefono = new javax.swing.JTextField();
        buttonModificar = new javax.swing.JButton();
        buttonCancelar = new javax.swing.JButton();
        labelEstado = new javax.swing.JLabel();
        labelCiudad = new javax.swing.JLabel();
        labelCentro = new javax.swing.JLabel();
        labelNombre = new javax.swing.JLabel();
        labelTelefono = new javax.swing.JLabel();
        labelDireccion = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        campoDireccion = new javax.swing.JTextArea();

        setTitle("Modificar Centro de Distribución");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Modificar Centro de Distribución", javax.swing.border.TitledBorder.CENTER, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11), new java.awt.Color(0, 102, 204))); // NOI18N

        comboEstado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboEstadoActionPerformed(evt);
            }
        });

        comboCiudad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboCiudadActionPerformed(evt);
            }
        });

        comboNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboNombreActionPerformed(evt);
            }
        });

        buttonModificar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        buttonModificar.setText("Modificar");
        buttonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonModificarActionPerformed(evt);
            }
        });

        buttonCancelar.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        buttonCancelar.setText("Cancelar");
        buttonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonCancelarActionPerformed(evt);
            }
        });

        labelEstado.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelEstado.setText("Estado");

        labelCiudad.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelCiudad.setText("Ciudad");

        labelCentro.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelCentro.setText("Centro Distribución");

        labelNombre.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelNombre.setText("Nombre Centro");

        labelTelefono.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelTelefono.setText("Telefono");

        labelDireccion.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        labelDireccion.setText("Dirección");

        campoDireccion.setColumns(20);
        campoDireccion.setRows(5);
        jScrollPane1.setViewportView(campoDireccion);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelNombre)
                    .addComponent(labelEstado)
                    .addComponent(labelCentro)
                    .addComponent(labelTelefono)
                    .addComponent(labelDireccion)
                    .addComponent(labelCiudad)
                    .addComponent(buttonModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(comboNombre, 0, 216, Short.MAX_VALUE)
                            .addComponent(campoNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                            .addComponent(comboCiudad, javax.swing.GroupLayout.Alignment.TRAILING, 0, 216, Short.MAX_VALUE)
                            .addComponent(comboEstado, javax.swing.GroupLayout.Alignment.TRAILING, 0, 216, Short.MAX_VALUE)
                            .addComponent(campoTelefono, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 216, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(19, 19, 19))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(42, 42, 42))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(comboEstado, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboCiudad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelCiudad))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(comboNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelCentro))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelNombre))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(campoTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(labelTelefono)))
                    .addComponent(labelEstado))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelDireccion)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 22, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonModificar)
                    .addComponent(buttonCancelar))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void comboEstadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboEstadoActionPerformed
        int select = -1;
        select = this.comboEstado.getSelectedIndex();
        Lugar estadoDueño = new Lugar();
        estadoDueño = estados.elementAt(select);
        cargarCiudades(estadoDueño);
    }//GEN-LAST:event_comboEstadoActionPerformed

    private void comboCiudadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCiudadActionPerformed
        int select = -1;
       //select = this.comboCiudad.getSelectedIndex();
        if (this.comboCiudad.getItemCount()>0){
            select = this.comboCiudad.getSelectedIndex();
            this.cargarCombito(select);
        }
    }//GEN-LAST:event_comboCiudadActionPerformed

    private void comboNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboNombreActionPerformed
        int select = -1;
        String nombre = "";
        String telefono = "";
        String direccion = "";
        if (this.comboNombre.getItemCount()>0){
            select = this.comboNombre.getSelectedIndex();
            CentroDistribucion centrico = new CentroDistribucion();
            centrico = vecCentrosAux.elementAt(select);
            nombre = centrico.getNombre();
            telefono = centrico.getTelefono();
            direccion = centrico.getDireccion();
            this.campoNombre.setText(nombre);
            this.campoTelefono.setText(telefono);
            this.campoDireccion.setText(direccion);
        }
    }//GEN-LAST:event_comboNombreActionPerformed

    private void buttonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonModificarActionPerformed
        int resultado = -1;

       this.controlGeneral =  new ControlGui ();
       resultado = this.controlGeneral.dialogoConfirmacion("¿Está seguro " +
       "que desea realizar esta operación?");

       if (resultado == 0){
           int select = -1;
           int codCentro = -1;
           String nombre = "";
           String telefono = "";
           String direccion = "";

           select = this.comboNombre.getSelectedIndex();
           CentroDistribucion centricoMod = new CentroDistribucion();
           centricoMod = vecCentrosAux.elementAt(select);

           codCentro = centricoMod.getCodigo();
           nombre = this.campoNombre.getText().toUpperCase();
           telefono = this.campoTelefono.getText().toUpperCase();
           direccion = this.campoDireccion.getText().toUpperCase();

           CentroDistribucion centrico = new CentroDistribucion(codCentro,nombre,
           telefono,direccion,1);

           this.controlCentro = new ControlGuiCentroDistribucion();
           controlCentro.modificarCentrodelSistema(centrico);
           this.cargarCombito(select);
       }
    }//GEN-LAST:event_buttonModificarActionPerformed

    private void buttonCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCancelarActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_buttonCancelarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run(Vector<Lugar> result) {
                new VentanaModificarCentro(result).setVisible(true);
            }
            public void run() {
                throw new UnsupportedOperationException("Not supported yet.");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonCancelar;
    private javax.swing.JButton buttonModificar;
    private javax.swing.JTextArea campoDireccion;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JTextField campoTelefono;
    private javax.swing.JComboBox comboCiudad;
    private javax.swing.JComboBox comboEstado;
    private javax.swing.JComboBox comboNombre;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCentro;
    private javax.swing.JLabel labelCiudad;
    private javax.swing.JLabel labelDireccion;
    private javax.swing.JLabel labelEstado;
    private javax.swing.JLabel labelNombre;
    private javax.swing.JLabel labelTelefono;
    // End of variables declaration//GEN-END:variables

    public void cargarCiudades(Lugar Estado){
        this.comboCiudad.removeAllItems();
        this.ciudadesAux.removeAllElements();

        int codigoEstado = -1;
        codigoEstado = Estado.getId();

        controlLugar = new ControlGuiLugar();
        ciudades = controlLugar.traerTodosLosLugares(2);

        for (Lugar lugar : ciudades) {
            if (lugar.getLugarID() == codigoEstado){
                ciudadesAux.addElement(lugar);
                this.comboCiudad.addItem(lugar.getNombrePropio());
            }
        }
    }

    public void cargarCombito(int select){
        this.vecCentros.removeAllElements();
        this.vecCentrosAux.removeAllElements();
        this.comboNombre.removeAllItems();

        this.controlCentro = new ControlGuiCentroDistribucion();
        this.vecCentros = controlCentro.traerTodosLosCentros();
        
        for (CentroDistribucion centro : vecCentros){
            if(centro.getLugarId() == ciudadesAux.elementAt(select).getId()){
                vecCentrosAux.addElement(centro);
                this.comboNombre.addItem(centro.getNombre());
            }
        }
    }
}
